name: "deploy / website"

on:
  workflow_run:
    workflows: ["trigger"]
    types: 
      - requested

permissions:
  statuses: write

concurrency:
  group: "${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}-${{ github.workflow_ref }}"
  cancel-in-progress: true
  

env:
  VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
  VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}

jobs:
  changes:
    name: "changes"
    runs-on: ubuntu-latest
    outputs:
      should_run: ${{ steps.changes.outputs.should_run }}
      sha: ${{ steps.changes.outputs.sha }}
      merge_sha: ${{ steps.changes.outputs.merge_sha }}
      pr_number: ${{ steps.changes.outputs.pr_number }}
      source_branch: ${{ steps.changes.outputs.source_branch }}
      source_repo: ${{ steps.changes.outputs.source_repo }}
      found_pr: ${{ steps.changes.outputs.found_pr }}
      mergeable: ${{ steps.changes.outputs.mergeable }}
    steps:
      - uses: actions/checkout@v4
      - uses: "gradio-app/gradio/.github/actions/changes@main"
        id: changes
        with:
          type: "website"
          token: ${{ secrets.GITHUB_TOKEN }}
          commit_status: false
  comment-deploy-start:
    needs: changes
    uses: "./.github/workflows/comment-queue.yml"
    secrets:
      gh_token: ${{ secrets.COMMENT_TOKEN }}
    with:
      pr_number: ${{ needs.changes.outputs.pr_number }}
      message: website~pending~null
  deploy-website:
    name: "deploy-website"
    runs-on: ubuntu-latest
    needs: changes
    if: needs.changes.outputs.should_run == 'true'
    outputs:
      vercel_url: ${{ steps.output_url.outputs.vercel_url }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ needs.changes.outputs.merge_sha }}
          repository: ${{ needs.changes.outputs.mergeable == 'true' &&  github.repository || needs.changes.outputs.source_repo }}
      - name: install dependencies
        uses: "gradio-app/gradio/.github/actions/install-all-deps@main"
        with:
          always_install_pnpm: true
          skip_build: true
      - name: deploy json to aws
        if: needs.changes.outputs.source_branch == 'changeset-release/main' && needs.changes.outputs.source_repo == 'gradio-app/gradio'
        run: |
          export AWS_ACCESS_KEY_ID=${{ secrets.AWSACCESSKEYID }}
          export AWS_SECRET_ACCESS_KEY=${{ secrets.AWSSECRETKEY }}
          export AWS_DEFAULT_REGION=us-west-2
          version=$(jq -r .version js/_website/src/lib/json/version.json)
          aws s3 cp ./js/_website/src/lib/json/ s3://gradio-docs-json/$version/ --recursive
      - name: Install Vercel CLI
        run: pnpm install --global vercel@latest
      # preview
      - name: Pull Vercel Environment Information
        shell: bash
        if: needs.changes.outputs.found_pr == 'true'
        run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
      - name: Build Project Artifacts
        if: needs.changes.outputs.found_pr == 'true'
        shell: bash
        run: vercel build --token=${{ secrets.VERCEL_TOKEN }}
      - name: Deploy Project Artifacts to Vercel
        if: needs.changes.outputs.found_pr == 'true'
        id: output_url
        shell: bash
        run: echo "vercel_url=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }})" >> $GITHUB_OUTPUT
      # production
      - name: Pull Vercel Environment Information
        if: needs.changes.outputs.source_repo == 'gradio-app/gradio' && needs.changes.outputs.source_branch == 'main'
        shell: bash
        run: vercel pull --yes --environment=production --token=${{ secrets.VERCEL_TOKEN }}
      - name: Build Project Artifacts
        if: needs.changes.outputs.source_repo == 'gradio-app/gradio' && needs.changes.outputs.source_branch == 'main'
        shell: bash
        run: vercel build --prod --token=${{ secrets.VERCEL_TOKEN }}
      - name: Deploy Project Artifacts to Vercel
        if: needs.changes.outputs.source_repo == 'gradio-app/gradio' && needs.changes.outputs.source_branch == 'main'
        shell: bash
        run: echo "VERCEL_URL=$(vercel deploy --prebuilt --prod --token=${{ secrets.VERCEL_TOKEN }})" >> $GITHUB_ENV
     

  comment-deploy-success:
    uses: "./.github/workflows/comment-queue.yml"
    needs: [deploy-website, changes]
    if: needs.deploy-website.result == 'success' && needs.changes.outputs.found_pr == 'true'
    secrets:
      gh_token: ${{ secrets.COMMENT_TOKEN }}
    with:
      pr_number: ${{ needs.changes.outputs.pr_number }}
      message: website~success~${{needs.deploy-website.outputs.vercel_url}}
  comment-deploy-failure:
    uses: "./.github/workflows/comment-queue.yml"
    needs: [deploy-website, changes]
    if: always() && needs.deploy-website.result == 'failure' && needs.changes.outputs.found_pr == 'true'
    secrets:
      gh_token: ${{ secrets.COMMENT_TOKEN }}
    with:
      pr_number: ${{ needs.changes.outputs.pr_number }}
      message: website~failure~https://github.com/gradio-app/gradio/actions/runs/${{github.run_id}}/
    
